package com.wyl.template.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wyl.template.aop.annotation.SysLogger;
import com.wyl.template.domain.common.PageResult;
import com.wyl.template.domain.common.ResponseResult;
import com.wyl.template.domain.dto.UserRoleDTO;
import com.wyl.template.domain.excel.UserExcel;
import com.wyl.template.domain.model.SysUserDO;
import com.wyl.template.domain.request.UserPageRequest;
import com.wyl.template.domain.vo.PermissionVO;
import com.wyl.template.domain.vo.UserRoleVO;
import com.wyl.template.domain.vo.UserVO;
import com.wyl.template.enums.LoggerType;
import com.wyl.template.service.SysPermissionService;
import com.wyl.template.service.SysUserRoleService;
import com.wyl.template.service.SysUserService;
import com.wyl.template.util.ExcelUtil;
import com.wyl.template.util.ObjConverter;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @Auther: wyl
 * @Date: 2021/6/11
 * @Description:
 */
@SysLogger(group = "用户")
@RestController
@RequestMapping("/user")
public class UserController extends BaseController<SysUserService, SysUserDO> {

    @Autowired
    private SysUserService userService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private SysUserRoleService userRoleService;

    /**
     * 获得当前用户信息
     *
     * @return
     */
    @SysLogger(name = "查询用户信息", type = LoggerType.SELECT)
    @GetMapping("currentUser")
    public ResponseResult currentUser() {
        Subject subject = SecurityUtils.getSubject();
        SysUserDO user = (SysUserDO) subject.getPrincipal();
        if (user != null) {
            return ResponseResult.success(ObjConverter.convert(user, UserVO.class));
        } else {
            return ResponseResult.error("获得用户信息失败！");
        }
    }

    /**
     * 获得当前用户的权限列表
     *
     * @return
     */
    @SysLogger(name = "获得当前用户的权限列表", type = LoggerType.SELECT)
    @GetMapping("selectMenuListByUser")
    public ResponseResult selectMenuListByUser() {
        Subject subject = SecurityUtils.getSubject();
        SysUserDO user = (SysUserDO) subject.getPrincipal();
        List<PermissionVO> resList = permissionService.listMenuListByUser(user.getId());
        if (resList != null) {
            return ResponseResult.success(resList);
        } else {
            return ResponseResult.error("获得菜单失败！");
        }

    }

    /**
     * 查询分页信息
     *
     * @param userPageRequest
     * @return
     */
    @SysLogger(name = "分页查询", type = LoggerType.SELECT)
    @GetMapping("user")
    public ResponseResult getUsers(UserPageRequest userPageRequest) {
        ResponseResult<IPage<SysUserDO>> selectPage = this.selectPage(userPageRequest);
        IPage pageInfo = selectPage.getData();
        List<SysUserDO> userList = pageInfo.getRecords();

        List<UserRoleVO> userRoleVOList = userRoleService.listUserRolesIn(userList);

/*        Page<UserRoleVO> pageResult = new Page<UserRoleVO>(
                selectPage.getData().getCurrent(),
                selectPage.getData().getSize(),
                selectPage.getData().getTotal())
                .setRecords(userRoleVOList);*/

        PageResult pageResult = new PageResult();
        BeanUtils.copyProperties(pageInfo, pageResult);
        pageResult.setRecords(userRoleVOList);
        return ResponseResult.success(pageResult);
    }

    /**
     * 新增用户
     *
     * @param userRoleDTO
     * @return
     */
    @SysLogger(name = "新增", type = LoggerType.ADD)
    @PostMapping("user")
    public ResponseResult addUser(@RequestBody UserRoleDTO userRoleDTO) {
        return userService.addUser(userRoleDTO);
    }

    /**
     * 修改用户
     *
     * @param userRoleDTO
     * @return
     */
    @SysLogger(name = "修改", type = LoggerType.UPDATE)
    @PutMapping("user")
    public ResponseResult updateUser(@RequestBody UserRoleDTO userRoleDTO) {
        return userService.updateUser(userRoleDTO);
    }

    /**
     * 删除用户
     *
     * @param userPageRequest
     * @return
     */
    @SysLogger(name = "删除", type = LoggerType.DELETE)
    @DeleteMapping("user")
    public ResponseResult deleteUser(@RequestBody UserRoleDTO userPageRequest) {
        return userService.deleteUser(userPageRequest.getId());
    }

    /**
     * 修改密码
     *
     * @param oldPassword
     * @param newPassword
     * @return
     */
    @SysLogger(name = "修改密码", type = LoggerType.UPDATE)
    @PostMapping("updatePassword")
    public ResponseResult updatePassword(@RequestParam("oldPassword") String oldPassword, @RequestParam("newPassword") String newPassword) {
//        HttpServletRequest req;
//        String oldPassword = req.getParameter("oldPassword");
//        String newPassword = req.getParameter("newPassword");
        return userService.updatePassword(oldPassword, newPassword);
    }


    /**
     * 重置密码
     *
     * @param id 用户id
     * @return
     */
    @SysLogger(name = "重置密码", type = LoggerType.UPDATE)
    @PostMapping("resetPassword")
    public ResponseResult resetPassword(@RequestParam("id") Long id) {
        return userService.resetPassword(id);
    }


    /**
     * 导出用户信息
     *
     * @param response
     * @param userPageRequest
     */
    @SysLogger(name = "导出用户信息", type = LoggerType.EXPORT)
    @GetMapping("/download")
    public void download(UserPageRequest userPageRequest, HttpServletResponse response) {
        ResponseResult<IPage<SysUserDO>> selectPage = this.selectPage(userPageRequest);
        IPage pageInfo = selectPage.getData();
        List<SysUserDO> userList = pageInfo.getRecords();

        List<UserExcel> userExcelList = userList.stream().map(user -> {
            UserExcel userExcel = new UserExcel();
            BeanUtils.copyProperties(user, userExcel);
            userExcel.setSex(user.getSex() == 1 ? "男" : "女");
            return userExcel;
        }).collect(Collectors.toList());


        ExcelUtil.downloadExcel(response, "用户列表", userExcelList, UserExcel.class);
    }
}
